package com.distribution.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.exception.ServiceException;
import com.distribution.framework.config.ServerConfig;
import com.distribution.system.domain.entity.CmccDaOperatorFlow;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.req.CmccDaOperatorFlowReq;
import com.distribution.system.domain.req.CmccDaProductListReq;
import com.distribution.system.service.CmccDaProductService;
import com.distribution.system.service.CmccDaOperatorFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 地区分流商品增删改成操作
 *
 * @since 2023-05-20 20:46:37
 */
@RestController
@RequestMapping(value = "/cmccDaOperatorFlow")
@Slf4j
public class CmccDaOperatorFlowController extends BaseController {
    @Resource
    CmccDaOperatorFlowService cmccDaOperatorFlowService;

    @Autowired
    private CmccDaProductService cmccDaProductService;

    @PostMapping("/getOperatorFlowInfoByProductId")
    public List<CmccDaOperatorFlow> getOperatorFlowInfoByProductId(@RequestBody Integer productId){
        return cmccDaOperatorFlowService.getOperatorFlowInfoListByProductId(productId);
    }

    /**
     * 异网分流商品列表查询
     */
    @PostMapping("/operatorFlowProductList")
    public TableDataInfo operatorFlowProductList(@RequestBody CmccDaProductListReq cmccDaProductListReq) {
        List<CmccDaProduct> productListByQueryParam = cmccDaProductService.getProductListByQueryParam(cmccDaProductListReq);
        List<CmccDaOperatorFlow> cmccDaDistanceFlowList = cmccDaOperatorFlowService.list();
        //根据cardid去重
        List<Integer> idList = cmccDaDistanceFlowList.stream().map(CmccDaOperatorFlow::getProductId).collect(Collectors.toList());
        List<CmccDaProduct> cmccDaProductList = productListByQueryParam.stream().filter(x -> idList.contains(x.getId())).collect(Collectors.toList());
        if (cmccDaProductList.isEmpty()) throw new ServiceException("查询不到分流信息");
        startPage();
        return getDataTable(cmccDaProductList);
    }

    /**
     * 添加地区分流商品
     */
    @PostMapping("/add")
    AjaxResult add(@RequestBody @Valid CmccDaOperatorFlowReq cmccDaOperatorFlowReq) {
        cmccDaOperatorFlowService.addOperatorFlowInfo(cmccDaOperatorFlowReq.getCurrentId(), cmccDaOperatorFlowReq.getOperatorFlowId());
        return success();
    }

    /**
     * 删除分流商品
     */
    @PostMapping("/cancelOperatorFlowByProductId")
    AjaxResult cancelOperatorFlowByProductId(@RequestBody Integer productId) {
        //System.out.println(id);
        List<CmccDaOperatorFlow> list = cmccDaOperatorFlowService.list(new LambdaQueryWrapper<CmccDaOperatorFlow>().eq(CmccDaOperatorFlow::getProductId, productId));
        if (list.isEmpty()) return error("数据不存在");
        List<Integer> idList = list.stream().map(CmccDaOperatorFlow::getId).collect(Collectors.toList());
        cmccDaProductService.lambdaUpdate().eq(CmccDaProduct::getId, productId).set(CmccDaProduct::getOperatorFlowFlag, "0").update();
        return cmccDaOperatorFlowService.removeByIds(idList) ? success() : error("删除失败");
    }

    /**
     * 删除分流商品
     */
    @PostMapping("/delOperatorFlowInfoById")
    AjaxResult del(@RequestBody @Valid Integer id) {
        //删除指定id的分流商品
        return cmccDaOperatorFlowService.removeById(id) ? success() : error("删除失败");
    }
}
